package cn.hnu.qq.client.panel;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;

import cn.hnu.qq.client.controller.ListenServerMessageThread;
import cn.hnu.qq.client.controller.SearchController;
import cn.hnu.qq.client.event.MessageEvent;
import cn.hnu.qq.client.listener.MessageListener;
import cn.hnu.qq.entity.User;
import cn.hnu.qq.entity.message.Message;
import cn.hnu.qq.entity.message.ResponseSearchMessage;
import cn.hnu.qq.global.MessageType;

import com.jgoodies.forms.factories.FormFactory;
import com.jgoodies.forms.layout.ColumnSpec;
import com.jgoodies.forms.layout.FormLayout;
import com.jgoodies.forms.layout.RowSpec;
import javax.swing.ImageIcon;

public class SearchFriendsPanel extends JPanel implements MessageListener,ActionListener{
	private JTextField textField;
	
	private UserInfoPanel userInfoPanel;
	
	private SearchController controller = SearchController.getInstance();
	/**
	 * Create the panel.
	 */
	@Override
	protected void paintComponent(Graphics g) {
		Image image = null;
		try {
			image = ImageIO.read(new File("src/main/java/image/02.jpg"));
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		};
			
		g.drawImage(image, 0, 0, this.getWidth(), this.getHeight(), this);

	}
	public SearchFriendsPanel() {
		setLayout(null);
		this.setPreferredSize(new Dimension(308, 216));
		JPanel panel = new JPanel(){
			@Override
			protected void paintComponent(Graphics g) {
				Image image = null;
				try {
					image = ImageIO.read(new File("src/main/java/image/02.jpg"));
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				};
						
				g.drawImage(image, 0, 0, this.getWidth(), this.getHeight(), this);

			}
		};
		panel.setBounds(10, 10, 288, 84);
		add(panel);
		panel.setLayout(new FormLayout(new ColumnSpec[] {
				FormFactory.RELATED_GAP_COLSPEC,
				FormFactory.DEFAULT_COLSPEC,
				FormFactory.RELATED_GAP_COLSPEC,
				FormFactory.DEFAULT_COLSPEC,
				FormFactory.RELATED_GAP_COLSPEC,
				FormFactory.DEFAULT_COLSPEC,
				FormFactory.RELATED_GAP_COLSPEC,
				FormFactory.DEFAULT_COLSPEC,
				FormFactory.RELATED_GAP_COLSPEC,
				ColumnSpec.decode("default:grow"),},
			new RowSpec[] {
				FormFactory.RELATED_GAP_ROWSPEC,
				FormFactory.DEFAULT_ROWSPEC,
				FormFactory.RELATED_GAP_ROWSPEC,
				FormFactory.DEFAULT_ROWSPEC,
				FormFactory.RELATED_GAP_ROWSPEC,
				FormFactory.DEFAULT_ROWSPEC,}));
		
		JLabel lblAccount = new JLabel("account:");
		panel.add(lblAccount, "4, 4");
		
		textField = new JTextField();
		panel.add(textField, "8, 4, left, default");
		textField.setColumns(15);
		
		JButton btnNewButton = new JButton("search");
		panel.add(btnNewButton, "10, 4, center, center");
		btnNewButton.setActionCommand("search");
		btnNewButton.addActionListener(this);
		JPanel panel_1 = new JPanel(){
			@Override
			protected void paintComponent(Graphics g) {
				Image image = null;
				try {
					image = ImageIO.read(new File("src/main/java/image/02.jpg"));
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				};
						
				g.drawImage(image, 0, 0, this.getWidth(), this.getHeight(), this);

			}
		};
		panel_1.setBounds(10, 104, 288, 100);
		add(panel_1);
		panel_1.setLayout(new BorderLayout(0, 0));
		
		JLabel lblNewLabel = new JLabel("");
		lblNewLabel.setIcon(new ImageIcon(SearchFriendsPanel.class.getResource("/image/xiaohui2.png")));
		panel_1.add(lblNewLabel, BorderLayout.WEST);
		userInfoPanel = new UserInfoPanel();
		panel_1.add(userInfoPanel,BorderLayout.CENTER);
		ListenServerMessageThread.getInstance().addMessageListener(this);
	}

	public String getSearchAccount() {
		return textField.getText();
	}

	public void messageArrived(MessageEvent event) {
		Message message = event.getSource();
		if(message.getMessageType() == MessageType.SEARCH_USER){
			ResponseSearchMessage rsp = (ResponseSearchMessage) message;
			User user = (User) rsp.getScource();
			if(user == null){
				return;
			}
			userInfoPanel.setUser(user);
		
		}
	}

	public void actionPerformed(ActionEvent e) {
		if(e.getActionCommand().equals("search")){
			String account = getSearchAccount();
			if(account == null || account.equals("")){
				JOptionPane.showMessageDialog(this, "请输入账号！！");
				return;
			}
			controller.searchUser(account);
		}
	}

	public String getSearchedAccount() {
		return userInfoPanel.getAccount();
	}
}
